/*
https://leetcode-cn.com/problems/online-election/solution/zai-xian-xuan-ju-by-leetcode-solution-4835/
 */
import java.util.HashMap;
import java.util.Map;

public class Solution911 {

    int[] record;
    int[] times;

    public Solution911(int[] persons, int[] times) {
        Map<Integer,Integer> map=new HashMap<>();
        record=new int[persons.length];
        int max=0,winner=0;
        for (int i=0;i<persons.length;i++){
            int vote=map.getOrDefault(persons[i],0)+1;
            map.put(persons[i],vote);
            if (vote>=max) {
                max = vote;
                winner = persons[i];
            }
            record[i]=winner;
        }
        this.times=times;
    }

    public int q(int t) {
        int l=0;
        int r=times.length-1;
        if (t<times[l]){
            return 0;
        }else{
            while (l<r){
                int mid=(l+r)/2;
                if (t>=times[mid]){
                    l=mid;
                }else{
                    r=mid-1;
                }
                if (r-l==1){
                    if (t>=times[r]){
                        l=r;
                    }
                    break;
                }
            }
        }
        return record[l];
    }

    public static void main(String[] args) {
        Solution911 solution911=new Solution911(new int[]{0,1,1,0,0,1,0},new int[]{0,5,10,15,20,25,30});
        System.out.println(solution911.q(3));
        System.out.println(solution911.q(12));
        System.out.println(solution911.q(25));
        System.out.println(solution911.q(15));
        System.out.println(solution911.q(24));
        System.out.println(solution911.q(8));
    }
}
